﻿using System;
using System.IO;
using System.Reflection;
using System.Xml;
using DataStore.Domain;
using DataStore.Repositories;

namespace DataStore
{
    public partial class Database
    {

        /// <summary>
        /// Default data for centain entities
        /// </summary>
        public static class DefaultData
        {
            /// <summary>
            /// Writes all the entity data
            /// </summary>
            /// <param name="unitOfWork">The unit of work.</param>
            public static void Write(UnitOfWork unitOfWork)
            {
                try
                {
                    unitOfWork.BeginTransaction();

                    WriteCategories(unitOfWork);
                    WriteKeywords(unitOfWork);
                    WritePublishingFormat(unitOfWork);
                    WriteCountry(unitOfWork);
                    //WriteLanguage(unitOfWork);

                    unitOfWork.CommitTransaction();
                }
                catch (Exception)
                {
                    unitOfWork.RollbackTransaction();
                    throw;
                }
            }

            /// <summary>
            /// Writes the categories entity data.
            /// </summary>
            /// <param name="unitOfWork">The unit of work.</param>
            public static void WriteCategories(UnitOfWork unitOfWork)
            {
                Category[] categories = 
                    {
                        new Category() { Text = "Fiction" }, 
                        new Category() { Text = "Technical" },
                    };


                IRepository<Category> categoryRepository = new CategoryRepository(unitOfWork);
                foreach (var category in categories)
                {
                    categoryRepository.Save(category);
                }
                
            }

            /// <summary>
            /// Writes the keywords entity data.
            /// </summary>
            /// <param name="path">The path.</param>
            public static void WriteKeywords(UnitOfWork unitOfWork)
            {
                Keyword[] keywords =
                {
                    new Keyword() { Text = "book" }, 
                    new Keyword() { Text = "favorite" },
                };

                IRepository<Keyword> keywordRepository = new KeywordRepository(unitOfWork);
                foreach (var keyword in keywords)
                {
                    keywordRepository.Save(keyword);
                }
            }

            public static void WritePublishingFormat(UnitOfWork unitOfWork)
            {
                PublishingFormat[] formats =
                {
                    new PublishingFormat() { Text = "Paperback" }, 
                    new PublishingFormat() { Text = "Hardcover" }, 
                };

                IRepository<PublishingFormat> repository = new PublishingFormatRepository(unitOfWork);
                foreach (PublishingFormat format in formats)
                {
                    repository.Save(format);
                }
            }

            public static void WriteCountry(UnitOfWork unitOfWork)
            {
                const string countryResourceName = @"DataStore.DefaultData.Countries.xml";

                Assembly assembly = Assembly.GetExecutingAssembly();
                XmlDocument doc = new XmlDocument();
                using (Stream countriesStream = assembly.GetManifestResourceStream(countryResourceName))
                {
                    doc.Load(countriesStream);
                }


                CountryRepository repository1 = new CountryRepository(unitOfWork);
                foreach (XmlNode node in doc.DocumentElement.ChildNodes)
                {
                    Country country = new Country()
                                          {
                                              Id = new Guid(node.SelectSingleNode("Guid").InnerText),
                                              Name = node.SelectSingleNode("Name").InnerText,
                                              Code = node.SelectSingleNode("Code").InnerText,
                                          };
                    repository1.Save(country);
                }
            }

            public static void WriteLanguage(UnitOfWork unitOfWork)
            {

                const string languageResourceName = @"DataStore.DefaultData.Languages.xml";

                Assembly assembly = Assembly.GetExecutingAssembly();
                XmlDocument doc = new XmlDocument();
                using (Stream languagesStream = assembly.GetManifestResourceStream(languageResourceName))
                {
                    doc.Load(languagesStream);
                }
                IRepository<Language> repository = new LanguageRepository(unitOfWork);
                foreach (XmlNode node in doc.DocumentElement.ChildNodes)
                {
                    Language language = new Language()
                                            {
                                                Id = new Guid(node.SelectSingleNode("Guid").InnerText),
                                                Name = node.SelectSingleNode("Name").InnerText,
                                            };
                    repository.Save(language);
                }
            }
        }
    }
}
